﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Chizx.Web.e
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //开发模式
            ////根据用户对象的上下文得到用户对象信息，用于得到其他信息
            //string _userName = "2013115020208";
            //string _Password = "123123";
            //Chizx.BLL.View.zx_view_user vubll = new Chizx.BLL.View.zx_view_user();
            ////验证登录信息，检测用户数据是否存在
            //Chizx.Model.View.zx_view_user _currentUser = vubll.GetModelByID_ByPWD(_userName, _Password);
            //string _degree = degreeRadioList.SelectedItem.Value;
            //string _roles = _currentUser.zu_roles;
            //Session["UserDegree"] = "A0";
            //Chizx.BLL.UserBll bll = new Chizx.BLL.UserBll();
            //Chizx.Model.UserModel model = bll.GetModel(_currentUser.zu_id, (int)_currentUser.zc_id);
            //model.zu_lastdate = DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss").ToString();
            //model.zu_lastip = Chizx.Common.IPHelper.getIPAddress();
            //bll.Update(model);
            //_currentUser.zu_lastdate = model.zu_lastdate;
            //_currentUser.zu_lastip = model.zu_lastip;
            ////保存当前用户对象信息
            //FormsAuthentication.SetAuthCookie(_userName, false);
            ////1.处理用户实体
            //Session["UserEntity"] = _currentUser;
            //if (Session["returnPage"] != null)
            //{
            //    string returnpage = Session["returnPage"].ToString();
            //    Session["returnPage"] = null;
            //    Response.Redirect(returnpage);
            //}
            //else
            //{
            //    Response.Redirect("/e/Index.aspx");
            //}

            //生产模式
            if (!this.IsPostBack)//不是为响应而回传的加载
            {
                this.ViewState["GUID"] = System.Guid.NewGuid().ToString();
                this.lblGUID.Text = this.ViewState["GUID"].ToString();
            }
        }
        
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if ((Session["PassErrorCountAdmin"] != null) && (Session["PassErrorCountAdmin"].ToString() != ""))
            {
                int PassErroeCount = Convert.ToInt32(Session["PassErrorCountAdmin"]);
                if (PassErroeCount > 3)
                {
                    txtUsername.Disabled = true;
                    txtPass.Disabled = true;
                    btnLogin.Enabled = false;
                    this.lblMsg.Text = "对不起，你错误登录了三次，系统登录锁定！";
                    return;
                }
            }

            #region 检查验证码
            if ((Session["CheckCode"] != null) && (Session["CheckCode"].ToString() != ""))
            {
                if (Session["CheckCode"].ToString().ToLower() != this.CheckCode.Value.ToLower())
                {
                    this.lblMsg.Text = "所填写的验证码与所给的不符 !";
                    Session["CheckCode"] = null;
                    return;
                }
                else
                {
                    Session["CheckCode"] = null;
                }
            }
            else
            {
                Response.Redirect("/e/Login.aspx");
            }
            #endregion

            string _userName = Maticsoft.Common.PageValidate.InputText(txtUsername.Value.Trim(), 30);
            string _Password = Maticsoft.Common.PageValidate.InputText(txtPass.Value.Trim(), 30);
            Chizx.BLL.View.zx_view_user vubll = new Chizx.BLL.View.zx_view_user();

            //验证登录信息，检测用户数据是否存在
            Chizx.Model.View.zx_view_user _currentUser = vubll.GetModelByID_ByPWD(_userName, _Password);
            if (_currentUser == null)//用户名或密码不存在
            {
                this.lblMsg.Text = "登陆失败：用户名或密码错误！请重新输入！";
                if ((Session["PassErrorCountAdmin"] != null) && (Session["PassErrorCountAdmin"].ToString() != ""))
                {
                    int PassErroeCount = Convert.ToInt32(Session["PassErrorCountAdmin"]);
                    Session["PassErrorCountAdmin"] = PassErroeCount + 1;
                }
                else
                {
                    Session["PassErrorCountAdmin"] = 1;
                }
            }
            else //用户名或密码正确
            {
                //根据用户对象的上下文得到用户对象信息，用于得到其他信息
                string _degree = degreeRadioList.SelectedItem.Value;
                string _roles = _currentUser.zu_roles;
                if (_roles == "SS" && _degree == "S") 
                {//学生 S:SS  
                    Session["UserDegree"] = "SS";
                }
                else if (_roles == "SX" && _degree == "S")
                {//实习生 S:SX  
                    Session["UserDegree"] = "SX";
                }
                else if (_roles != "SS" && _roles != "SX" && _degree == "A")
                {//管理员用户 A:A*
                    Session["UserDegree"] = _roles;
                }
                else
                {
                    this.lblMsg.Text = "身份权限错误！请重新选择认证身份！";
                    return;
                }
                //用户数据更新
                Chizx.BLL.UserBll bll = new Chizx.BLL.UserBll();
                Chizx.Model.UserModel model = bll.GetModel(_currentUser.zu_id, (int)_currentUser.zc_id);
                if (model.zu_lastdate == null || model.zu_lastdate == "")
                {
                    Chizx.Model.MessageModel mm = new Chizx.Model.MessageModel();
                    Chizx.BLL.MessageBll mb = new Chizx.BLL.MessageBll();
                    mm.zu_id = model.zu_id;
                    mm.zmsg_topic = "请完善您的详细信息";
                    mm.zmsg_context = "尊敬的用户 " + model.zu_id + " ,系统检测到您是第一次登录,请及时完善您的详细信息！";
                    mm.zmsg_createdate = DateTime.Now;
                    mm.zmsg_status = "m0";
                    mb.Add(mm);
                }
                model.zu_lastdate = DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss").ToString();
                model.zu_lastip = Chizx.Common.IPHelper.getIPAddress();
                bll.Update(model);
                _currentUser.zu_lastdate = model.zu_lastdate;
                _currentUser.zu_lastip = model.zu_lastip;
                //保存当前用户对象信息
                FormsAuthentication.SetAuthCookie(_userName, false);
                //写入系统日志
                Chizx.BLL.SystemLogBll sbll = new Chizx.BLL.SystemLogBll();
                Chizx.Model.SystemLogModel slm = new Chizx.Model.SystemLogModel();
                slm.zu_id = model.zu_id;
                slm.zl_dowhen = DateTime.Now;
                slm.zl_dowhat = "登录系统";
                slm.zl_dotype = "1";//0:Test,1:Auth,2:Read,3:Write,4:Del,5:Inpor
                slm.zl_dodesc = "操作成功";
                sbll.Add(slm);
                //1.处理用户实体
                Session["UserEntity"] = model;
                if (Session["returnPage"] != null)
                {
                    string returnpage = Session["returnPage"].ToString();
                    Session["returnPage"] = null;
                    Response.Redirect(returnpage);
                }
                else
                {
                    Response.Redirect("/e/Index.aspx");
                }

            }
        }

        
    }
}